package ar.edu.uba.fi.jmit.graphvisualizer.server.storage;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

public class MonitorStorageTestCase extends TestCase {

	
	public void testGet() throws InterruptedException {
		PairTimeStorage<Map<String, String>> storage = new PairTimeStorage<Map<String, String>>(2);
		storage.add(new Date(), new HashMap<String, String>());
		storage.add(new Date(), new HashMap<String, String>());
		storage.add(new Date(), new HashMap<String, String>());
		storage.add(new Date(), new HashMap<String, String>());
		storage.add(new Date(), new HashMap<String, String>());
		assertEquals(5, storage.get().size());
		Thread.sleep(3000);
		assertEquals(0, storage.get().size());
	}
	
	public void testOldDates() throws InterruptedException {
		Calendar calendar = new GregorianCalendar();
		PairTimeStorage<Map<String, String>> storage = new PairTimeStorage<Map<String, String>>(2);
		storage.add(calendar.getTime(), new HashMap<String, String>());
		calendar.add(Calendar.SECOND, -5);
		storage.add(calendar.getTime(), new HashMap<String, String>());
		assertEquals(1, storage.get().size());
		Thread.sleep(3000);
		assertEquals(0, storage.get().size());
	}
	
	public void testSort() {
		Calendar calendar = new GregorianCalendar();
		PairTimeStorage<Map<String, String>> storage = new PairTimeStorage<Map<String, String>>(20);
		calendar.add(Calendar.SECOND, -5);
		storage.add(calendar.getTime(), new HashMap<String, String>());
		
		calendar.add(Calendar.SECOND, +3);
		storage.add(calendar.getTime(), new HashMap<String, String>());
		
		calendar.add(Calendar.SECOND, -2);
		storage.add(calendar.getTime(), new HashMap<String, String>());
		
		calendar.add(Calendar.SECOND, +5);
		storage.add(calendar.getTime(), new HashMap<String, String>());
		
		calendar.add(Calendar.SECOND, -1);
		storage.add(calendar.getTime(), new HashMap<String, String>());
		
		assertEquals(5, storage.get().size());
		List<Pair<Map<String, String>>> list = storage.get();
		Date firstDate = list.get(0).getDate();
		for(Pair<Map<String, String>> pair:list) {
			assertTrue(firstDate.compareTo(pair.getDate()) <=0);
		}
	}
}
